package com.fingard.dsp.bank.directbank.spdb12.util;

import com.guohualife.nrt.dto.req.download.WtpDownloadReqDTO;
import com.guohualife.nrt.dto.req.upload.WtpUploadReqDTO;
import com.guohualife.nrt.dto.res.download.WtpDownloadResDTO;
import com.guohualife.nrt.dto.res.upload.WtpUploadResDTO;
import com.guohualife.nrt.service.WsWtpFileTrans;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WtpFileUtil {
    public static String[] wtpUpload(String url, String fileName, String filePath) {
        String tmpStrRet[] = new String[]{"",""};
        //调用webservice上传接口
        List<String> fileNames = new ArrayList<String>();
        fileNames.add(fileName);

        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        factoryBean.setServiceClass(WsWtpFileTrans.class);
        factoryBean.setAddress(url);
        WsWtpFileTrans wtpFileTrans = (WsWtpFileTrans)factoryBean.create();
        WtpUploadReqDTO reqDTO = new WtpUploadReqDTO();
        reqDTO.setFileNameList(fileNames);
        reqDTO.setFilePath(filePath);
        reqDTO.setFileType("PL");
        List<WtpUploadResDTO> resDTOList = wtpFileTrans.fileUpload(reqDTO);
        String resultCode = resDTOList.get(0).getErrorCode();
        if ("US".equalsIgnoreCase(resultCode)||"UU".equalsIgnoreCase(resultCode)) {
            tmpStrRet[1] = "通知成功";
        } else {
            tmpStrRet[0] = "-1";
            tmpStrRet[1] = resDTOList.get(0).getErrorMsg();
        }
        return tmpStrRet;
    }

    public static Map<String, Object> wtpDownload(String url, String fileName) {
        Map<String, Object> tmpStrRet = new HashMap<String, Object>();

        //调用webservice上传接口
        List<String> fileNames = new ArrayList<String>();
        fileNames.add(fileName);
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        factoryBean.setServiceClass(WsWtpFileTrans.class);
        factoryBean.setAddress(url);
        WsWtpFileTrans wtpFileTrans = (WsWtpFileTrans)factoryBean.create();
        WtpDownloadReqDTO wtpDownloadReqDTO = new WtpDownloadReqDTO();
        wtpDownloadReqDTO.setFileName(fileName);
        wtpDownloadReqDTO.setFileType("PL");
        WtpDownloadResDTO wtpDownloadResDTO = wtpFileTrans.fileDownLoad(wtpDownloadReqDTO);
        String resultCode = wtpDownloadResDTO.getResultCode();
        if ("DS".equalsIgnoreCase(resultCode)) {
            // 成功
            Map<String,String> retMap = new HashMap<String, String>();
            retMap.put("wtpHost",wtpDownloadResDTO.getWtpHost());
            retMap.put("wtpPort",wtpDownloadResDTO.getWtpPort());
            retMap.put("userName",wtpDownloadResDTO.getUserName());
            retMap.put("passWord",wtpDownloadResDTO.getPassWord());
            retMap.put("filePath",wtpDownloadResDTO.getFilePath());
            retMap.put("FileName",wtpDownloadResDTO.getFileName());
            tmpStrRet.put("RespCode","");
            tmpStrRet.put("Result",retMap);
        } else {
            // 失败
            tmpStrRet.put("RespCode","-1");
            tmpStrRet.put("RespInfo",wtpDownloadResDTO.getResultCode() + "-" + wtpDownloadResDTO.getResultMsg());
        }
        return tmpStrRet;
    }
}